//
//  FileManager.m
//  tcpoctest
//
//  Created by gump on 12/30/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "FileManager.h"
#import "LocalPathDataSource.h"

@implementation FileManager

static FileManager *sharedInstance = nil;

+(FileManager*)shareControl
{
    if(!sharedInstance)
    {
        sharedInstance = [[super alloc] init];
    }
    return sharedInstance;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        NSString* pathpath = [[NSBundle mainBundle] pathForResource:@"Area" ofType:@"xml"];
        NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:pathpath];
        NSData* data = [file readDataToEndOfFile];
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //设置XML数据
        [parser setShouldProcessNamespaces:NO];
        [parser setShouldReportNamespacePrefixes:NO];
        [parser setShouldResolveExternalEntities:NO];
        
        [parser setDelegate:self];
        [parser parse];
    }
    
    return self;
}

-(void)createPath
{
#if !(TARGET_IPHONE_SIMULATOR)
    [[DataControl shareControl].localpathds setLPImagePath:
     [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
      stringByAppendingPathComponent:@"image"]];
    BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPImagePath]
                                        withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setLPSoundPath:
     [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
      stringByAppendingPathComponent:@"sound"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPSoundPath]
                                        withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");

    
    [[DataControl shareControl].localpathds setLPFHeadImagePath:
    [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
     stringByAppendingPathComponent:@"headimage"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPHeadImagePath]
                                        withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setLPChatPath:
     [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
      stringByAppendingPathComponent:@"chat"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPChatPath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setFircirPath:
     [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
      stringByAppendingPathComponent:@"fircir"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPFircirPath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setGiftPath:
     [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
      stringByAppendingPathComponent:@"gift"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPGiftPath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
#else
    NSString *localpath = CUSTOMPATH;
    
    [[DataControl shareControl].localpathds setLPImagePath:
     [NSString stringWithFormat:@"%@%@",localpath,@"image"]];
    BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPImagePath]
                                        withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setLPSoundPath:[NSString stringWithFormat:@"%@%@",localpath,@"sound"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPSoundPath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setLPFHeadImagePath:
     [NSString stringWithFormat:@"%@%@",localpath,@"headimage"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPHeadImagePath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setLPChatPath:[NSString stringWithFormat:@"%@%@",localpath,@"chat"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPChatPath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setFircirPath:[NSString stringWithFormat:@"%@%@",localpath,@"fircir"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPFircirPath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");
    
    [[DataControl shareControl].localpathds setGiftPath:[NSString stringWithFormat:@"%@%@",localpath,@"gift"]];
    bo = [[NSFileManager defaultManager] createDirectoryAtPath:[[DataControl shareControl].localpathds LPGiftPath]
                                   withIntermediateDirectories:YES attributes:nil error:nil];
    NSAssert(bo,@"创建目录失败");

#endif

}

-(NSString *)updateHeadImage:(UIImage *)image
{
    NSString *newPath = [NSString stringWithFormat:@"%@/%@",
                         [[DataControl shareControl].localpathds LPHeadImagePath],
                         getFileNameWithDate(FT_HEADIMAGE)];
    
    NSData *data = UIImageJPEGRepresentation(image, JPG_COMPRESS_RATE);
    
    [data writeToFile:newPath atomically:YES];
    
    return newPath;
}

-(void)deletePathFile:(NSString *)path
{
    NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:
                                      path];
    NSString *fileName;
    while (fileName= [dirEnum nextObject])
    {
        [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@",
                                                          path,fileName]
                                                   error:nil];
    }

}

-(void)deleteFile:(NSString *)path
{
    [[NSFileManager defaultManager] removeItemAtPath:path
                                               error:nil];
}

-(NSString *)saveChatImage:(UIImage *)image
{
    NSString *newPath = [NSString stringWithFormat:@"%@/%@.jpg",
                         [[DataControl shareControl].localpathds LPChatPath],
                         getFileNameWithDate(FT_CHAT)];
    
    NSData *data = UIImageJPEGRepresentation(image, JPG_COMPRESS_RATE);
    
    [data writeToFile:newPath atomically:YES];
    
    return newPath;
}

-(NSString *)saveChatData:(NSData *)data withIndex:(int)iindex
{
    NSString *newPath = [NSString stringWithFormat:@"%@/%@%d",
                         [[DataControl shareControl].localpathds LPChatPath],
                         getFileNameWithDate(FT_CHAT),
                         iindex];
    
    [data writeToFile:newPath atomically:YES];
    
    return newPath;
}

#pragma mark -xml parser-
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    static NSMutableArray *curarr = nil;
    
    if ([elementName isEqualToString:@"province"])
    {
        NSMutableDictionary  *pp = [[NSMutableDictionary alloc] init];
        [pp setValue:[attributeDict objectForKey:@"name"] forKey:@"State"];
        NSMutableArray *parr = [[NSMutableArray alloc]init];
        [pp setValue:parr forKey:@"Cities"];
        curarr = parr;
        
        [[DataControl shareControl].mapArray addObject:pp];
    }
    else if([elementName isEqualToString:@"city"])
    {
        NSMutableDictionary *cp = [[NSMutableDictionary alloc] init];
        [cp setValue:[attributeDict objectForKey:@"name"] forKey:@"city"];
        if (curarr != nil) {
            [curarr addObject:cp];
        }
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
}



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
}

@end
